
#include "main.h"
#include "lvgl_project.h"
#include "lv_conf.h"


char *p_gif1 = NULL;
char *p_gif2 = NULL;
bool load_gif(char **p,char *file);

static void init_task_handler(void *pvParameters)
{
	main_debug("-------------WELCOME TO GSM FM----------------\r\n");
	main_debug("编译时间:%s %s\r\n", __DATE__, __TIME__);
	main_debug("esp32 sdk version :%s\r\n", esp_get_idf_version());

	bsp_key_init();
	bsp_power_init();
	bsp_spk_sw_init();
	bsp_audio_select_init();

    system_data.spiffs_sta = my_spiffs_init();
	//system_data.sd_sta = bsp_sd_init();
	system_data.nvs_sta = nvs_init();
	//system_data.wm8978_sta = WM8978_Init();

	load_gif(&p_gif1,"/spiffs/power_on_gif_01.gif");
	load_gif(&p_gif2,"/spiffs/power_on_gif_02.gif");

	bsp_ledc_init();
	//IIC_Init();
	bsp_i2c2_init();
	sht20_init();
	rda5807_init();
	rda5807_task_init();

	system_init();
	wifi_init();

	ec11_task_init();
	multi_button_callback_init();
	multi_button_task_init();

	//font_init();//使用SD卡中字库，由于太卡暂时放弃
	//bsp_i2s_init();
	//i2s_test();
#if 1

	lcd_init(SPI3_HOST, 80000000);

    lvgl_init();
	lvgl_projet_init();
	lv_port_fs_init();
	lvgl_style_init();
	//lvgl_cursor_create();

	if(p_gif1 == NULL || p_gif2 == NULL)
	{
		lvgl_desktop_create(lv_scr_act());
	}else
	{
		lvgl_power_on_create(lv_scr_act());
	}

	//lv_demo_stress();
	//lv_demo_printer();
	//lv_demo_music();
	//lv_demo_widgets();

	bsp_ledc_set_duty(system_data.backlight);
	bsp_power_on();

#endif
	//bsp_uart_init();
#if 0
	rda5807_init();
	rda5807_enable(1);
	bsp_spk_sw_set(0);
	rda5807_mute_set(0);
	rda5807_vol_set(12);
	rda5807_freq_set(8910);  //频率设置
	rda5807_seekth_set(5);
	rda5807_seek_direction(0);
	main_debug("频率:%d",rda5807_freg_get());

	uint8_t seek_sta=0;
	uint32_t time=0;
	while (1)
	{
		//rda5807_init();
		//printf_memory();

		//bsp_i2c1_addr_test();
		if(bsp_key_read_power_gpio())
		{
			main_debug("开始搜台...");
			seek_sta = rda5807_seek_channel();
			main_debug("搜台结果:%d",seek_sta);
		}
		if(++time>100)
		{
			time = 0;
			main_debug("频率:%d",rda5807_freg_get());
			main_debug("信号强度:%d",rda5807_rssi_get());
		}

		vTaskDelay(10 / portTICK_PERIOD_MS);
	}
#endif

	vTaskDelete(NULL);
}

void app_main()
{
	xTaskCreate(init_task_handler,
				"init_task_handler",
				1024 * 20,
				NULL,
				1,
				NULL);
	// while(1)
	// {
	// 	//lv_obj_set_style_local_bg_color(lv_scr_act(), LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
	// 	//printf_memory();

	// 	main_debug("剩余内存:%d 历史最低内存:%d 最大可申请单次块:%d",
	// 	heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
	// 	heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL),
	// 	heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL)
	// 	);

	// 	vTaskDelay(1000 / portTICK_PERIOD_MS);
	// }
	vTaskDelete(NULL);
}

void get_memory(char *file,int len)
{
    int free_size;
    int free_size2;

    ESP_LOGW("内存监控", "%s:%d",file,len);
    free_size = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
	free_size2 = heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
    printf("内部RAM剩余%d.%dKB  外部PSRAM剩余%d.%dkB", free_size/1024,free_size%1024,free_size2/1024,free_size2%1024);
}




long get_file_size(FILE *stream)
{
	long file_size = -1;
	long cur_offset = ftell(stream);	// 获取当前偏移位置
	if (cur_offset == -1) {
		main_debug("ftell failed :%s\n", strerror(errno));
		return -1;
	}
	if (fseek(stream, 0, SEEK_END) != 0) {	// 移动文件指针到文件末尾
		main_debug("fseek failed: %s\n", strerror(errno));
		return -1;
	}
	file_size = ftell(stream);	// 获取此时偏移值，即文件大小
	if (file_size == -1) {
		main_debug("ftell failed :%s\n", strerror(errno));
	}
	if (fseek(stream, cur_offset, SEEK_SET) != 0) {	// 将文件指针恢复初始位置
		main_debug("fseek failed: %s\n", strerror(errno));
		return -1;
	}
	return file_size;
}
bool load_gif(char **p,char *file)
{
	bool r_dat = false;
	long size=0,r_size=0;
	main_debug("加载文件:%s",file);
    FILE* f = fopen(file, "r");
    if (f != NULL)
	{
        main_debug("打开成功");
		size = get_file_size(f);
		main_debug("文件大小:%ld",size);
		*p = malloc(size);
		if(*p != NULL)
		{
			main_debug("内存申请成功");
			r_size = fread(*p, 1, size, f);
			if(r_size == size)
			{
				main_debug("读取成功");
			}
			r_dat = true;
		}else
		{
			main_debug("内存申请失败");
		}
		fclose(f);
    }
	return r_dat;
}
